﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using WP09S03_OfficeTracking.Controller;
using WP09S03_OfficeTracking.DAL;
using WP09S03_OfficeTracking.DAL.DSItemTableAdapters;


namespace WP09S03_OfficeTracking.UserControls
{
    public partial class IssueTransactionUC : UserControl
    {
        ItemController item = new ItemController();
        TransactionController tran = new TransactionController();
        private CategoryTableAdapter adpCate;
        public IssueTransactionUC()
        {
            InitializeComponent();
            adpCate = new CategoryTableAdapter();
            Init();
        }

        private void Init()
        {
            DataBinding_Category();
            CheckConsumable();
            this.cbbStatus.SelectedIndex = 0;
        }
        private void DataBinding_Category()
        {
            DSItem.CategoryDataTable table = new DSItem.CategoryDataTable();
            table = adpCate.GetDataByConsumable(this.rbYes.Checked ? 1 : 0);
            this.cbbCategory.DataSource = table;
            this.cbbCategory.DisplayMember = table.CategoryNameColumn.ColumnName;
            this.cbbCategory.ValueMember = table.CategoryIDColumn.ColumnName;
        }

        private void DataBinding_IssueTransaction(string issueTranID, string status, string consumable, System.Nullable<System.DateTime> usedStartTimeFrom, System.Nullable<System.DateTime> usedStartTimeTo, System.Nullable<System.DateTime> usedEndTimeFrom, System.Nullable<System.DateTime> usedEndTimeTo, string employeeID, string employeeName, string itemID, string itemName, string categoryID, string brand, string model)
        {

            DSTransaction.vw_IssueTransactionDataTable table = tran.GetIssueTransaction(issueTranID, status, consumable, usedStartTimeFrom, usedStartTimeTo, usedEndTimeFrom, usedEndTimeTo, employeeID, employeeName, itemID, itemName, categoryID, brand, model);
            table.Columns.Remove(table.CategoryIDColumn.ColumnName);
            this.dgvData.DataSource = table;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                BindData_DGV();

            }
            catch (Exception)
            {
                throw;
            }
        }

        private void rb_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckConsumable();
                DataBinding_Category();

            }
            catch (Exception)
            {
                throw;
            }
        }

        private void CheckConsumable()
        {
            if (this.rbYes.Checked)
            {
                this.cbbStatus.SelectedIndex = 0;
                this.cbbStatus.Enabled = false;
            }
            else
            {
                this.cbbStatus.Enabled = true;
            }
        }

        private void cbIssueDate_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.cbIssueDate.Checked)
                {
                    this.dtpIssueDateFrom.Enabled = true;
                    this.dtpIssueDateTo.Enabled = true;
                }
                else
                {
                    this.dtpIssueDateFrom.Enabled = false;
                    this.dtpIssueDateTo.Enabled = false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void cbReturnDate_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.cbReturnDate.Checked)
                {
                    this.dtpReturnDateFrom.Enabled = true;
                    this.dtpReturnDateTo.Enabled = true;
                }
                else
                {
                    this.dtpReturnDateFrom.Enabled = false;
                    this.dtpReturnDateTo.Enabled = false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void BindData_DGV()
        {
            string issueTranID = this.txtTransactionID.Text.Trim();
            string status = this.cbbStatus.SelectedIndex == 0 ? "" : this.cbbStatus.SelectedItem.ToString();
            string consumable = this.rbYes.Checked || this.rbNo.Checked ? (this.rbYes.Checked ? "Y" : "N") : "";
            System.Nullable<System.DateTime> usedStartTimeFrom=null;
            System.Nullable<System.DateTime> usedStartTimeTo=null;
            System.Nullable<System.DateTime> usedEndTimeFrom=null;
            System.Nullable<System.DateTime> usedEndTimeTo=null;
            if (this.cbIssueDate.Checked)
            {
                usedStartTimeFrom =Convert.ToDateTime(this.dtpIssueDateFrom.Text);
                usedStartTimeTo = Convert.ToDateTime(this.dtpIssueDateTo.Text);
            }
            if (this.cbReturnDate.Checked)
            {
                usedEndTimeFrom = Convert.ToDateTime(this.dtpReturnDateFrom.Text);
                usedEndTimeTo = Convert.ToDateTime(this.dtpReturnDateTo.Text);
            }
            string employeeID = this.txtEmployeeID.Text.Trim();
            string employeeName = this.txtEmployeeName.Text.Trim();
            string itemID = this.txtItemID.Text.Trim();
            string itemName = this.txtItemName.Text.Trim();
            string categoryID = this.cbbCategory.SelectedValue.ToString();
            string brand = this.txtBrand.Text.Trim();
            string model = this.txtModel.Text.Trim();
            DataBinding_IssueTransaction(issueTranID, status, consumable, usedStartTimeFrom, usedStartTimeTo, usedEndTimeFrom, usedEndTimeTo, employeeID, employeeName, itemID, itemName, categoryID, brand, model);
        }

    }
}
